﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;

namespace DacSanMienTayOBJ
{
    public class TypeArticleBusiness
    {
        public static bool InsertTypeArticle(TypeArticle artType, ConnectionType typeConnect)
        {
            Sqlhelper helper = new Sqlhelper(typeConnect);
            try
            {
                bool rs = false;
                string sql = "insert into typearticle(TypeName,Metakeyword,IsVisible,Priority) values(@typeName,@metaKey,@isVisible,@priority)";
                SqlParameter[] pa = new SqlParameter[4];
                pa[0] = new SqlParameter("@typeName", artType.TypeName);
                pa[1] = new SqlParameter("@metaKey",artType.MetaKeyword);
                pa[2] = new SqlParameter("@isVisible",artType.IsVisible);
                pa[3] = new SqlParameter("@priority",artType.Priority);
                SqlCommand command = helper.GetCommand(sql, pa, false);
                int row = command.ExecuteNonQuery();
                if (row > 0)
                {
                    rs = true;
                }
                return rs;
            }
            catch (Exception ex)
            {
                Utilitys.WriteLog(ex.Message);
                return false;
            }
            finally
            {
                helper.destroy();
            }
        }

        public static bool UpdateTypeArticle(int typeID,TypeArticle artType, ConnectionType typeConnect)
        {
            Sqlhelper helper = new Sqlhelper(typeConnect);
            try
            {
                bool rs = false;
                string sql = "update typearticle TypeName=@typeName,Metakeyword=@metaKey where TypeID=@typeID";
                SqlParameter[] pa = new SqlParameter[3];
                pa[0] = new SqlParameter("@typeName", artType.TypeName);
                pa[1] = new SqlParameter("@metaKey", artType.MetaKeyword);
                pa[2] = new SqlParameter("@typeID", typeID);
                SqlCommand command = helper.GetCommand(sql, pa, false);
                int row = command.ExecuteNonQuery();
                if (row > 0)
                {
                    rs = true;
                }
                return rs;
            }
            catch (Exception ex)
            {
                Utilitys.WriteLog(ex.Message);
                return false;
            }
            finally
            {
                helper.destroy();
            }
        }

        public static bool UpdateTypeArticleVisible(int typeID,int visible, ConnectionType typeConnect)
        {
            Sqlhelper helper = new Sqlhelper(typeConnect);
            try
            {
                bool rs = false;
                string sql = "update typearticle IsVisible=@isVisible where TypeID=@typeID";
                SqlParameter[] pa = new SqlParameter[2];
                pa[0] = new SqlParameter("@isVisible", visible);
                pa[1] = new SqlParameter("@typeID", typeID);
                SqlCommand command = helper.GetCommand(sql, pa, false);
                int row = command.ExecuteNonQuery();
                if (row > 0)
                {
                    rs = true;
                }
                return rs;
            }
            catch (Exception ex)
            {
                Utilitys.WriteLog(ex.Message);
                return false;
            }
            finally
            {
                helper.destroy();
            }
        }

        public static List<TypeArticle> ListAllTypeArticle(ConnectionType typeConnect)
        {
            Sqlhelper helper = new Sqlhelper(typeConnect);
            try
            {
                List<TypeArticle> list = new List<TypeArticle>();
                string sql = "select *from typearticle";
                SqlCommand command = helper.GetCommandNonParameter(sql, false);
                SqlDataReader reader = command.ExecuteReader();
                while (reader.Read())
                {
                    TypeArticle artType = new TypeArticle();
                    artType.TypeID = int.Parse(reader["TypeID"].ToString());
                    artType.TypeName = reader["TypeName"].ToString();
                    artType.MetaKeyword = reader["Metakeyword"].ToString();
                    artType.IsVisible = int.Parse(reader["IsVisible"].ToString());
                    artType.Priority = int.Parse(reader["Priority"].ToString());
                    list.Add(artType);
                }

                return list;
            }
            catch (Exception ex)
            {
                Utilitys.WriteLog(ex.Message);
                return null;
            }
            finally
            {
                helper.destroy();
            }
        }

        public static List<TypeArticle> ListAllTypeArticleVisible(int visible,ConnectionType typeConnect)
        {
            Sqlhelper helper = new Sqlhelper(typeConnect);
            try
            {
                List<TypeArticle> list = new List<TypeArticle>();
                string sql = "select *from typearticle where IsVisible=@isVisible";
                SqlParameter[] pa = new SqlParameter[1];
                pa[0] = new SqlParameter("@isVisible",visible);
                SqlCommand command = helper.GetCommand(sql,pa, false);
                SqlDataReader reader = command.ExecuteReader();
                while (reader.Read())
                {
                    TypeArticle artType = new TypeArticle();
                    artType.TypeID = int.Parse(reader["TypeID"].ToString());
                    artType.TypeName = reader["TypeName"].ToString();
                    artType.MetaKeyword = reader["Metakeyword"].ToString();
                    artType.IsVisible = int.Parse(reader["IsVisible"].ToString());
                    artType.Priority = int.Parse(reader["Priority"].ToString());
                    list.Add(artType);
                }

                return list;
            }
            catch (Exception ex)
            {
                Utilitys.WriteLog(ex.Message);
                return null;
            }
            finally
            {
                helper.destroy();
            }
        }

        public static TypeArticle GetypeArticleDetail(int typeID, ConnectionType typeConnect)
        {
            Sqlhelper helper = new Sqlhelper(typeConnect);
            try
            {
                TypeArticle artType = null;
                string sql = "select *from typearticle where TypeID=@typeID";
                SqlParameter[] pa = new SqlParameter[1];
                pa[0] = new SqlParameter("@typeID",typeID);
                SqlCommand command = helper.GetCommand(sql, pa, false);
                SqlDataReader reader = command.ExecuteReader();
                if (reader.Read())
                {
                    artType = new TypeArticle();
                    artType.TypeID = int.Parse(reader["TypeID"].ToString());
                    artType.TypeName = reader["TypeName"].ToString();
                    artType.MetaKeyword = reader["Metakeyword"].ToString();
                    artType.IsVisible = int.Parse(reader["IsVisible"].ToString());
                    artType.Priority = int.Parse(reader["Priority"].ToString());
                   
                }

                return artType;
            }
            catch (Exception ex)
            {
                Utilitys.WriteLog(ex.Message);
                return null;
            }
            finally
            {
                helper.destroy();
            }
        }
    }
}
